package com.jr.controller;

import com.google.gson.Gson;
import com.jr.pojo.ProjectInfo;
import com.jr.service.IProjectInfoService;
import com.jr.service.impl.ProjectInfoServiceImpl;
import com.jr.util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/projectInfo/*")
public class ProjectInfoController extends HttpServlet {

    private IProjectInfoService ips = new ProjectInfoServiceImpl();

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI(); // 获取完整请求路径  --- /aaa/projectInfo/showdt
        int index = requestURI.lastIndexOf("/") + 1;
        String mName = requestURI.substring(index); // 截取最后一个"/"之后的字符串作为方法名
        /* ***********************/
        if (mName.length() == 0) {
            mName = "showdt"; // 如果方法名为空（URL以 / 结尾），默认使用showdt
        }
        /* 反射获取并调用对应方法 */
        Class<ProjectInfoController> cla = ProjectInfoController.class; // 获取当前类的Class对象
        Method method = null;
        try {
            // 查找当前类中是否存在指定名称、且参数为HttpServletRequest和HttpServletResponse的方法
            method = cla.getMethod(mName, HttpServletRequest.class, HttpServletResponse.class);

            /*response.setContentType("text/html;charset=UTF-8");
            request.setCharacterEncoding("UTF-8");*/

            // 调用找到的方法，this表示当前类实例，传递request和response作为参数
            method.invoke(this, request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace(); // 找不到对应方法的处理（这会导致404错误）
        } catch (IllegalAccessException e) {
            e.printStackTrace(); // 方法访问权限不足的处理
        } catch (InvocationTargetException e) {
            e.printStackTrace(); // 方法调用过程中抛出异常的处理
        }
    }

    public void showdt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        response.setContentType("application/json;charset=UTF-8");
        String status = request.getParameter("status"); // 获取前端传递的status参数
        int proStatus = 0;
        if (status == null) {
            proStatus = -1;
        } else {
            proStatus = Integer.parseInt(status);
        }

        PageHelper ph = new PageHelper();
        ph.setDateCount(ips.queryDataCount(proStatus));//获得总条数
        String indexpage = request.getParameter("indexpage");//当前是第几页
        if (indexpage == null) {
            ph.setPageIndex(1);//当前是第几页
        } else {
            ph.setPageIndex(Integer.parseInt(indexpage));//当前是第几页
        }
        ArrayList<ProjectInfo> projectInfos = ips.queryByStatus(proStatus, ph);
        ph.setListIndex(projectInfos);
        response.getWriter().println(new Gson().toJson(ph));

    }

    public void queryById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        int pid = Integer.parseInt(request.getParameter("pid"));
        ProjectInfo projectInfo = ips.queryByPid(pid);
        response.getWriter().println(new Gson().toJson(projectInfo));

    }

    public void updatePro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int status = Integer.parseInt(request.getParameter("status"));
        int pid = Integer.parseInt(request.getParameter("pid"));
        ProjectInfo pro = new ProjectInfo();
        pro.setPid(pid);
        pro.setStatus(status);
        boolean b = ips.updateProject(pro);
        response.getWriter().println(new Gson().toJson(b));

    }
}
